package com.atguigu.guli.service.edu.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseQuery;
import com.atguigu.guli.service.edu.entity.vo.CoursePublishVo;
import com.atguigu.guli.service.edu.entity.vo.CourseVo;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.VideoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2020-10-14
 */
@RestController

@Slf4j
@Api(tags = "课程管理")
@RequestMapping("/admin/edu/course")
public class AdminCourseController {
    @Autowired(required = true)
    private CourseService courseService;

    @Autowired
    private VideoService videoService;

    @ApiOperation("根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }



    @ApiOperation("根据Id删除课程")
    @DeleteMapping("remove/{id}")
    public R removeCourseByid(@PathVariable String id){
        //TODO 删除视频：VOD
        //在此处调用vod中的删除视频文件的接口
        //删除视频：VOD
        videoService.removeMediaVideoByCourseId(id);

        //删除封面：OSS
        courseService.removeCoverById(id);
        //删除课程
        boolean result = courseService.removeCourseById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }


    @ApiOperation("根据Id获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R selectCoursePublishVoById(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.selectCoursePublishById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item", coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页课程列表")
    @GetMapping("list/{page}/{limit}")
    public R index(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(value = "查询对象")
                    CourseQuery courseQuery){

        IPage<CourseVo> pageModel = courseService.selectPage(page, limit, courseQuery);
        List<CourseVo> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }




    @ApiOperation("新增课程")
    @PutMapping("update-course-info/{id}")
    public R updateCourseById(@PathVariable("id") String id,@RequestBody CourseInfoForm courseInfoForm){
        courseService.updateCoureseByid(id,courseInfoForm);

        return R.ok().message("课程修改成功");
    };



    @ApiOperation("根据id查询课程")
    @GetMapping("course-info/{id}")
    public R selectCourseById(@PathVariable("id") String id){
        log.warn("根据id查询课程的方法被调用了");
        CourseInfoForm courseInfoForm =courseService.selectCourseById(id);
        return R.ok().data("item",courseInfoForm);
    }


    @ApiOperation("新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourseInfo(courseInfoForm);

//
//        Course course = new Course();
//        CourseDescription courseDescription = new CourseDescription();
//        BeanUtils.copyProperties(courseInfoForm,course);
//        courseService.save(course);
//        String id = course.getId();
//        courseDescription.setId(id);
//        courseDescription.setDescription(courseInfoForm.getDescription());
//        courseDescriptionService.save(courseDescription);
        return R.ok().data("courseId", courseId).message("课程保存成功");
    };
}

